<?php

namespace app\api\controller\v1;

use think\Controller;
use app\validate\Register as RegisterValidate;
use app\validate\Login as LoginValidate;
use app\service\User as UserService;
use think\facade\Cache;
use think\facade\Request;

Class Login extends Controller
{
    // 账号登录
    public function index()
    {
        $post = input('param.');

        $validate = new LoginValidate();
        if (!$validate->check($post)) {
            $this->error($validate->getError());
        }

        $ip = get_ip();
        $redis = 'login_'.$post['phone'];
        $this->frequent($redis);

        $u_servier = new UserService();
        $map = [ 'phone' => $post['phone'] ];
        $find = $u_servier->u_model->where($map)->find();
        if (!empty($find)) {
            $map['password'] = md5($post['password'].strtotime($find['create_time']));
            if ($u_servier->u_model->where($map)->count() <= 0) {
                $this->error('账号或密码错误');
            }

            if ($find['status'] !== 0) {
                $this->error('您的账号已被冻结');
            }

            $u_servier->u_model->save(['last_login_ip' => $ip], ['id' => $find['id']]);
            $result = [
                'token' => buildJwt(['id' => $find['uid']])
            ];
            Cache::rm($redis);
            $this->success('登录成功，正在为您连接大厅...', '', $result);
        } else {
            $this->error('账号或密码错误');
        }
    }

    // 账号注册
    public function register()
    {
        $post = input('post.');

        $validate = new RegisterValidate();
        if (!$validate->check($post)) {
            $this->error($validate->getError());
        }

        $ip = get_ip();
        $redis = 'register_'.$ip;
        if (empty(Cache::get($redis))) {
            Cache::set($redis, 1, 30*60);
        } else {
            if ((int)Cache::get($redis) > 10) {
                $this->error('操作过于频繁，请30分钟后再试');
            }
            Cache::inc($redis);
        }

        $u_servier = new UserService();

        // 判断电话是否存在异常
        $find = $u_servier->u_model->where(['phone' => $post['phone']])->find();
        if (!empty($find)) {
            $this->error('账号已存在');
        }

        $time = time();
        $post['uid'] = $u_servier->buildId();
        $post['password'] = md5($post['password'].$time);
        $post['create_time'] = $time;
        $post['register_ip'] = $ip;
        $post['last_login_ip'] = $ip;
        $post['headimg'] = mt_rand(1, 6);
        if ($u_servier->u_model->save($post) > 0) {
            $result = [
                'token' => buildJwt(['id' => $post['uid']])
            ];
            $this->success('注册成功，正在为您连接大厅...', '', $result);
        } else {
            $this->error('网络异常，注册失败请重试');
        }
    }

    // 错误操作
    public function frequent($redis)
    {
        if (empty(Cache::get($redis))) {
            Cache::set($redis, 1, 30*60);
        } else {
            if ((int)Cache::get($redis) > 10) {
                $this->error('操作过于频繁，请30分钟后再试');
            }
            Cache::inc($redis);
        }
    }
}
